package com.dcivision.framework.taglib.channel;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.MessageResourcesFactory;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.Utility;
import com.dcivision.framework.taglib.utility.SourceManage;

public class ChannelColumnTag extends BodyTagSupport {
  
  private static final Log log = LogFactory.getLog(ChannelColumnTag.class);
  
  private boolean icon = false;
  private String column = null;
  private String valign = "top";
  private String align = null;
  private int height = -1;
  private int width = -1;
  private String classStyle = null;
  private String style = null;
  private boolean separator = true;

  public int doStartTag() throws JspException{

    String content = getContent();
    try {
      pageContext.getOut().println(content);
    } catch (IOException e) {
      
      log.error(e, e);
    }

    return SKIP_BODY;
  }

  public int doEndTag() {
    return EVAL_PAGE;
  }

  private String getContent(){
    SessionContainer sessionContainer = (SessionContainer)this.pageContext.getSession().getAttribute(GlobalConstant.SESSION_CONTAINER_KEY);
    String columnStr = "";
    if(!Utility.isEmpty(column)) {
      columnStr=MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), column);
    }

    StringBuffer bstr = new StringBuffer();
    bstr.append("                            <th");
    if(!Utility.isEmpty(align)){
        bstr.append(" align=\""+align+"\"");
    }
    if(!Utility.isEmpty(classStyle)){
        bstr.append(" class=\""+classStyle+"\"");
    }
    bstr.append(" nowrap>");
    bstr.append("<div class=\"");
    if(this.separator){
      bstr.append("sep");
    }else{
      bstr.append("firstsep");
    }
    bstr.append("\">");
    if(!Utility.isEmpty(columnStr)){
      if(this.getIcon()) {
        bstr.append("<img src='"+SourceManage.getImgSrc(pageContext, column, true, false)+"' border=0>");
      } else{
        if("home.label.message_id".equals(column)){
          bstr.append("<input type=\"checkbox\" name=\"allSelectedID\" onclick=\"selectAllCheck(\'basicSelectedID\',this);checkButtonClass(\'markButton\',\'basicSelectedID\')\">");
        }else{
          bstr.append(columnStr);
        }
      }
    }else{
      bstr.append("&nbsp");
    }
    bstr.append("</div>");
    bstr.append("</th>");

    return bstr.toString();
  }

  public void release(){
    super.release();
    icon = false;
    column = null;
    style = null;
    this.align = "center";
    this.valign = "top";
    this.width = -1;
    this.height = -1;
    this.classStyle = "tabletitle";
  }

  public boolean getIcon() {
    return icon;
  }

  public void setIcon(boolean icon) {
    this.icon = icon;
  }

  public String getColumn() {
    return column;
  }

  public void setColumn(String column) {
    this.column = column;
  }

  public String getStyle() {
    return style;
  }

  public void setStyle(String style) {
    this.style = style;
  }

  public String getAlign() {
    return align;
  }

  public void setAlign(String align) {
    this.align = align;
  }

  public String getClassStyle() {
    return classStyle;
  }

  public void setClassStyle(String classStyle) {
    this.classStyle = classStyle;
  }

  public int getHeight() {
    return height;
  }

  public void setHeight(int height) {
    this.height = height;
  }

  public String getValign() {
    return valign;
  }

  public void setValign(String valign) {
    this.valign = valign;
  }

  public int getWidth() {
    return width;
  }

  public void setWidth(int width) {
    this.width = width;
  }

  public void setSeparator(boolean separator){
      this.separator = separator;
  }

  public boolean getSeparator(){
      return this.separator;
  }
}
